<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Models\Articles;
// use App\Models\Logs;

/* 
 * 返回文章详情
 */
class ArticleController extends Controller
{
    // 单独文章页
    public function article($id = 'new')
    {
        if ($id == 'new'){
            $article = DB::table('articles')->orderBy('aid', 'desc')->first();
            // $articles = DB::table('articles')->where('aid', $new->aid)->get();
            $title = '最新文章';
            $id = $article->aid;
        }else{
            $article = DB::table('articles')->where('aid', $id)->first();
        }

        $article->content = str_replace("`", "\`", $article->content);
        $classify = DB::table('classify')->get();
        return view('article', ['title'=>$id, 'article'=>$article, 'classify'=>$classify, 'id'=>$id]);
    }
    // 文章列表页(主页)
    public function articleList()
    {
        $articles = DB::table('articles')->get();
        $classify = DB::table('classify')->get();

        return view('home', ['articles'=>$articles, 'classify'=>$classify, 'title'=>'主页']);
    }


    // 文章管理列表页
    public function adminArticleList()
    {
        // 登录判断
        if (session('uEmail', 'null') == 'null') {
            return response('你还没有登录')->header('refresh', env('USER_WAIT', '5').';url='.env('APP_URL'));
        }
        
        $articlesList = DB::table('articles')->get();
        return view('admin.article', ['title'=>'文章管理', 'list'=>$articlesList]);
    }

    // 文章删除
    public function delete($id = null)
    {
        // 登录判断
        if (session('uEmail', 'null') == 'null') {
            return response('你还没有登录')->header('refresh', env('USER_WAIT', '5').';url='.env('APP_URL'));
        }

        if ($id == null) {
            return response('错误的参数{id}')->header('refresh', env('USER_WAIT', '5').';url='.env('APP_URL'));
        }
        $delete = DB::table('articles')->where('aid', $id)->delete();
        if ($delete != 0) {
            // 日志
            DB::table('logs')->insert([
                'cid' => '0',
                'uid' => session('uId')??'0',
                'operate' => '删除',
                'atitle' => '文章aid:'.$id,
                'date' => time()
            ]);
            return response('删除成功')->header('refresh', env('USER_WAIT', '5').';url='.env('APP_URL'));
        }else {
            return response('返回错误的值： '.$delete)->header('refresh', env('USER_WAIT', '5').';url='.env('APP_URL'));

        }
        
    }

    // 文章发布
    public function insert($id = null)
    {
        // 登录判断
        if (session('uEmail', 'null') == 'null') {
            return response('你还没有登录')->header('refresh', env('USER_WAIT', '5').';url='.env('APP_URL'));
        }

        // $articles = DB::table('articles')->where('aid', $id)->first();
        return view('insert', ['title'=>'文章发布'.$id]);
    }
    // 文章发布处理
    public function insertCheck($id = null)
    {
        // 登录判断
        if (session('uEmail', 'null') == 'null') {
            return response('你还没有登录')->header('refresh', env('USER_WAIT', '5').';url='.env('APP_URL'));
        }

        $cid = $_POST['cid']??'1';
        $uid = session('uId');
        $img = $_POST['img']??'/img/bg/2021.jpg';
        $img_alt = $_POST['img_alt']??'默认图片';
        $title1 = $_POST['title1']??'填充用文章';
        $title2 = $_POST['title2']??'填充用文章';
        $content = $_POST['content']??'填充用文章';

        // 发布
        $re = DB::table('articles')->insertGetId([
                'cid' => $cid,
                'uid' => $uid,
                'img' => $img,
                'img_alt' => $img_alt,
                'title' => $title1,
                'title2' => $title2,
                'content' => $content,
                'date' => time(),
                'lastdate' => time()
            ],'aid');
        
        if ($re != 0) {
            // 日志
            DB::table('logs')->insert([
                'cid' => '0',
                'uid' => session('uId')??'0',
                'operate' => '发布',
                'atitle' => '文章aid:'.$re,
                'date' => time()
            ]);
            return response('新增成功')->header('refresh', env('USER_WAIT', '5').';url='.env('APP_URL'));
        }else {
            return response('受影响的行： '.$re)->header('refresh', env('USER_WAIT', '5').';url='.env('APP_URL'));
        }
        // return response('updatec')->header('refresh', env('USER_WAIT', '5').';url='.env('APP_URL'));
    }

    // 文章修改
    public function update($id = null)
    {
        // 登录判断
        if (session('uEmail', 'null') == 'null') {
            return response('你还没有登录')->header('refresh', env('USER_WAIT', '5').';url='.env('APP_URL'));
        }

        if ($id == null) {
            $id = $_GET['id']??0;
        }

        if ($id == 0) {
            return response('错误的参数id')->header('refresh', env('USER_WAIT', '5').';url='.env('APP_URL'));

        }
        $article = DB::table('articles')->where('aid', $id)->first();
        $classify = DB::table('classify')->get();
        return view('update', ['title'=>'文章编辑'.$id, 'article'=>$article, 'classify'=>$classify]);
    }
    // 文章修改处理
    public function updateCheck($id = null)
    {
        // 登录判断
        if (session('uEmail', 'null') == 'null') {
            return response('你还没有登录')->header('refresh', env('USER_WAIT', '5').';url='.env('APP_URL'));
        }

        $cid = $_POST['cid']??'0';
        $aid = $_POST['aid'];
        $img = $_POST['img'];
        $img_alt = $_POST['img_alt'];
        $title1 = $_POST['title1'];
        $title2 = $_POST['title2'];
        $content = $_POST['content'];

        // return response(dump($content));//debug

        // 获取
        $update = DB::table('articles')
            ->where('aid', $aid)
            ->update([
                'cid' => $cid,
                'title' => $title1,
                'title2' => $title2,
                'content' => $content,
                'lastdate' => time(),
                'img' => $img,
                'img_alt' => $img_alt
            ]);
        if ($update != 0) {
            // 日志
            DB::table('logs')->insert([
                'cid' => '0',
                'uid' => session('uId')??'0',
                'operate' => '修改',
                'atitle' => '文章aid:'.$aid,
                'date' => time()
            ]);
            return response('更改成功')->header('refresh', env('USER_WAIT', '5').';url='.env('APP_URL'));
        }else {
            return response('返回错误的值： '.$update)->header('refresh', env('USER_WAIT', '5').';url='.env('APP_URL'));
        }

        // return response('updatec')->header('refresh', env('USER_WAIT', '5').';url='.env('APP_URL'));
    }

    // 删除所有文章
    public function deleteAll()
    {
        // 登录判断
        if (session('uEmail', 'null') == 'null') {
            return response('你还没有登录')->header('refresh', env('USER_WAIT', '5').';url='.env('APP_URL'));
        }
        // $delete = DB::table('articles')->truncate();
        
        // 日志
        DB::table('logs')->insert([
            'cid' => '0',
            'uid' => session('uId')??'0',
            'operate' => '删除',
            'atitle' => '所有文章',
            'date' => time()
        ]);
        return response('已删除所有文章')->header('refresh', env('USER_WAIT', '5').';url='.env('APP_URL'));
    }

    // 博客检索
    public function search()
    {
        $where = $_GET['q'];
        $articles = Articles::where('aid', 'like', "%$where%")
            ->orWhere('cid', 'like', "%$where%")
            ->orWhere('uid', 'like', "%$where%")
            ->orWhere('title', 'like', "%$where%")
            ->orWhere('title2', 'like', "%$where%")
            ->orWhere('content', 'like', "%$where%")
            ->get();
        $classify = DB::table('classify')->get();
        return view('home', ['articles'=>$articles, 'classify'=>$classify, 'title'=>'搜索', 'msg'=>'Search: '.$where]);

        // return response(dump($articles));

        // return response('未开发该功能')->header('refresh', env('USER_WAIT', '5').';url='.env('APP_URL'));
    }

    // 博客检索api
    public function searchAPI()
    {
        $where = $_GET['q'];
        $articles = Articles::where('aid', 'like', "%$where%")
            ->orWhere('cid', 'like', "%$where%")
            ->orWhere('uid', 'like', "%$where%")
            ->orWhere('title', 'like', "%$where%")
            ->orWhere('title2', 'like', "%$where%")
            ->orWhere('content', 'like', "%$where%")
            ->get();

        return view('api.search', ['articles'=>$articles, 'msg'=>$where]);

        // return response(dump($articles));

        // return response('未开发该功能')->header('refresh', env('USER_WAIT', '5').';url='.env('APP_URL'));
    }
    // 单独文章页api
    public function articleAPI($id = 'new')
    {
        if ($id == 'new'){
            $articles = DB::table('articles')->orderBy('aid', 'desc')->first();
            // $articles = DB::table('articles')->where('aid', $new->aid)->get();
            $title = '最新文章';
            $id = $articles->aid;
        }else{
            $articles = DB::table('articles')->where('aid', $id)->first();
        }
        $classify = DB::table('classify')->get();

        return view('api.article', ['title'=>$title, 'article'=>$articles, 'classify'=>$classify, 'id'=>$id]);
    }
}